package cn.icanci.loopstack.bic.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.*;

import cn.icanci.loopstack.bic.biz.service.NodeService;
import cn.icanci.loopstack.bic.common.model.TextValue;
import cn.icanci.loopstack.bic.common.model.base.NodeVO;
import cn.icanci.loopstack.bic.common.result.R;
import cn.icanci.loopstack.bic.web.form.NodeQueryForm;
import cn.icanci.loopstack.bic.web.mapper.NodeWebMapper;
import cn.icanci.loopstack.bic.web.model.Node;

/**
 * @author icanci
 * @since 1.0 Created in 2023/09/11 21:40
 */
@RestController
@RequestMapping("/bicAdmin/node")
public class NodeController {
    @Resource
    private NodeWebMapper nodeWebMapper;
    @Resource
    private NodeService   nodeService;

    // ================================ CRUD ================================
    @PostMapping("query")
    public R query(@RequestBody NodeQueryForm form) {
        return R.builderOk().data("queryPage", nodeService.queryPage(nodeWebMapper.web2vo(form.getNode()), form.getPaginator().getCurrentPage(), form.getPaginator().getPageSize()))
            .build();
    }

    @PostMapping("save")
    public R save(@RequestBody Node node) {
        nodeService.save(nodeWebMapper.web2vo(node));
        return R.builderOk().build();
    }

    @GetMapping("validateName/{systemUuid}/{name}")
    public R validateName(@PathVariable("systemUuid") String systemUuid, @PathVariable("name") String name) {
        NodeVO node = nodeService.queryBySystemUuidAndName(systemUuid, name);
        return R.builderOk().data("result", node == null).build();
    }

    @GetMapping("validateKey/{systemUuid}/{key}")
    public R validateKey(@PathVariable("systemUuid") String systemUuid, @PathVariable("key") String key) {
        NodeVO node = nodeService.queryBySystemUuidAndKey(systemUuid, key);
        return R.builderOk().data("result", node == null).build();
    }
    // ================================ Load Selectors ================================

    @GetMapping("loadSelector/{systemUuid}")
    public R loadSelector(@PathVariable("systemUuid") String systemUuid) {
        List<TextValue> textValues = nodeService.loadSelector(systemUuid);
        return R.builderOk().data("textValues", textValues).build();
    }

    @GetMapping("loadAllSelector")
    public R loadAllSelector() {
        List<TextValue> textValues = nodeService.loadAllSelector();
        return R.builderOk().data("textValues", textValues).build();
    }
}
